using System;
using System.Collections.Generic;
using System.Text;

using System.Net;
using System.Net.Sockets;

namespace Parsifal
{
    public class ClientPlusCollection : ManageGenericsArray<object>
    {

        private ManageGenericsArray<ClientPlus> iArClientPlus;

        private int mClientConnected = 0;

        public ClientPlusCollection()
        {
            //costruttore
            
        }

        public Socket[] GetSocketActiveFromCollection()
        {
            try
            {
                
                Socket[] arTemp = new Socket[1];
                

                ClientPlus res = null;
                for (int i = 0; i < this.Array.Count; i++)
                {
                    res = (ClientPlus)this.Array[i];
                    if (res!=null)
                    {
                        arTemp[i] = res.Socket;

                        System.Array.Resize(ref arTemp, arTemp.Length + 1);
                    }
                }

                return arTemp;
            }
            catch (Exception err)
            {

                throw err;
            }

            


        }



        public Socket[] GetSocketActiveFromCollectionWithoutSocket(Socket aSocketExcluded)
        {
            try
            {

                Socket[] arTemp = new Socket[2];


                ClientPlus res = null;
                for (int i = 0; i < this.Array.Count; i++)
                {
                    res = (ClientPlus)this.Array[i];
                    if (res != null)
                    {
                        if (res.Socket != aSocketExcluded)
                        {
                            arTemp[i] = res.Socket;

                            System.Array.Resize(ref arTemp, arTemp.Length + 1);
                        }
                    }
                }

                return arTemp;
            }
            catch (Exception err)
            {

                throw err;
            }




        }


        public ClientPlus[] GetClientPlusActiveFromCollection()
        {
            try
            {

                ClientPlus[] arTemp = new ClientPlus[1];


                ClientPlus res = null;
                for (int i = 0; i < this.Array.Count; i++)
                {
                    res = (ClientPlus)this.Array[i];
                    if (res != null)
                    {
                        arTemp[i] = res;

                        System.Array.Resize(ref arTemp, arTemp.Length + 1);
                    }
                }

                return arTemp;
            }
            catch (Exception err)
            {

                throw err;
            }




        }



        public ClientPlus GetActiveSocketBroadCasting()
        {
            try
            {

                ClientPlus iTemp = null;


                ClientPlus res = null;
                for (int i = 0; i < this.Array.Count; i++)
                {
                    res = (ClientPlus)this.Array[i];
                    if (res.isBroadcastServer == true)
                    {
                        iTemp = res;
 
                    }
                }

                return iTemp;
            }
            catch (Exception err)
            {
                return null;
                throw err;
            }




        }



        public int CountClientConnected
        {
            get
            {
                return mClientConnected;
            }
         
        }

       

        public ClientPlus GetClientPlusFromIndex(int aIndex)
        {

            try
            {
                ClientPlus res = null;
                for (int i=0;i<this.Array.Count;i++)
                {
                    res = (ClientPlus)this.Array[i];
                    if (res.Index == aIndex)
                    {
                        return res;
                    }
                }

                return res;
            }
            catch (Exception err)
            {
                
                throw err;
            }

        }


        public ClientPlus GetClientPlusFromNick(string nickServer)
        {

            try
            {
                ClientPlus res = null;
                for (int i = 0; i < this.Array.Count; i++)
                {
                    res = (ClientPlus)this.Array[i];
                    string nickToLower = res.NickName.ToLower();

                    if (nickToLower == nickServer)
                    {
                        return res;
                    }
                }

                return res;
            }
            catch (Exception err)
            {

                throw err;
            }

        }



        public ClientPlus GetClientPlusFromSocket(Socket aRemote)
        {

            try
            {
                ClientPlus res = null;
                for (int i = 0; i < this.Array.Count; i++)
                {
                    res = (ClientPlus)this.Array[i];
                    if (res.Socket == aRemote)
                    {
                        return res;
                    }
                }

                return res;
            }
            catch (Exception err)
            {

                throw err;
            }

        }



       


        public void AddElementToCollection(ClientPlus aElement)
        {
            int res = this.AddElementToArray(aElement);
            aElement.Index = res;
        }

        public void RemoveElementToCollection(int aIndex)
        {
            this.RemoveElementToArray(aIndex);
            mClientConnected -= 1;
        }


        public void RemoveElementToCollection(Socket remote)
        {
            try
            {
                ClientPlus res = null;
                for (int i = 0; i < this.Array.Count; i++)
                {
                    res = (ClientPlus)this.Array[i];
                    if (res.Socket == remote)
                    {
                        this.Array.Remove(res);
                    }
                }

            }
            catch (Exception err)
            {

                throw err;
            }

        }


    }
}
